
document.observe("dom:loaded", loadMainRegister);

function loadMainRegister() {

    var strUrlLocation = window.location.href;
    strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    var city = GetCookie("stateCity");
    $('form1').action = strUrlLink + city + "/register.aspx";

    //đăng ký
    //nút submit
    if ($("btnSignUp")) {
        $("btnSignUp").onclick = SignUp;
    }
    //chọn giới tính
    if ($("divcbMale")) {
        $("divcbMale").onclick = radioSignUp;
    }
    if ($("divcbFemale")) {
        $("divcbFemale").onclick = radioSignUp;
    }
    //sự kiện enter
    if ($("txtSUName")) {
        $("txtSUName").onkeypress = PressKey;
    }
    if ($("txtSUNick")) {
        $("txtSUNick").onkeypress = PressKey;
    }
    if ($("txtSUEmail")) {
        $("txtSUEmail").onkeypress = PressKey;
    }
    if ($("txtSUPass")) {
        $("txtSUPass").onkeypress = PressKey;
    }
    if ($("txtSURePass")) {
        $("txtSURePass").onkeypress = PressKey;
    }
    if ($("ddlDate")) {
        $("ddlDate").onkeypress = PressKey;
    }
    if ($("ddlMonth")) {
        $("ddlMonth").onkeypress = PressKey;
    }
    if ($("ddlYear")) {
        $("ddlYear").onkeypress = PressKey;
    }
    if ($("txtCapcha")) {
        $("txtCapcha").onkeypress = PressKey;
    } 
    
    //sự kiện focus and lost focus
    if ($("txtSUName")) {
        $("txtSUName").onfocus = setTxtFocus;
        $("txtSUName").onblur = setTxtLostFocus;
    }
    if ($("txtSUNick")) {
        $("txtSUNick").onfocus = setTxtFocus;
        $("txtSUNick").onblur = setTxtLostFocus;
    }
    if ($("txtSUEmail")) {
        $("txtSUEmail").onfocus = setTxtFocus;
        $("txtSUEmail").onblur = setTxtLostFocus;
    }
    if ($("txtSUPass")) {
        $("txtSUPass").onfocus = setTxtFocus;
        $("txtSUPass").onblur = setTxtLostFocus;
    }
    if ($("txtSURePass")) {
        $("txtSURePass").onfocus = setTxtFocus;
        $("txtSURePass").onblur = setTxtLostFocus;
    }
    if ($("txtCapcha")) {
        $("txtCapcha").onfocus = setTxtFocus;
        $("txtCapcha").onblur = setTxtLostFocus;
    }
    //default focus
    if ($("txtSUName")) {
        $("txtSUName").focus();
    }
    //end

    //dang ky nhanh
    //nút submit
    if ($("btnSUBuy")) {
        $("btnSUBuy").onclick = SignUpBuy;
    }
    //sự kiện enter
    if ($("txtSUBName")) {
        $("txtSUBName").onkeypress = PressKey;
    }
    if ($("txtSUBEmail")) {
        $("txtSUBEmail").onkeypress = PressKey;
    }
    if ($("txtSUBPass")) {
        $("txtSUBPass").onkeypress = PressKey;
    }
    if ($("txtSUBRePass")) {
        $("txtSUBRePass").onkeypress = PressKey;
    }
    //sự kiện focus and lost focus
    if ($("txtSUBName")) {
        $("txtSUBName").onfocus = setTxtFocus;
        $("txtSUBName").onblur = setTxtLostFocus;
    }
    if ($("txtSUBEmail")) {
        $("txtSUBEmail").onfocus = setTxtFocus;
        $("txtSUBEmail").onblur = setTxtLostFocus;
    }
    if ($("txtSUBPass")) {
        $("txtSUBPass").onfocus = setTxtFocus;
        $("txtSUBPass").onblur = setTxtLostFocus;
    }
    if ($("txtSUBRePass")) {
        $("txtSUBRePass").onfocus = setTxtFocus;
        $("txtSUBRePass").onblur = setTxtLostFocus;
    }
    //default focus
    if ($("txtSUBName")) {
        $("txtSUBName").focus();
    }
    
    //end
}
//kiểm tra gới tính.
function radioSignUp(e) {
    var eID = utils.getElementFireEvent(e);
    $(eID.up("div")).select('div').invoke('removeClassName', 'checkedDK');
    $(eID.id).addClassName('checkedDK');
    $("cbMale").checked = (eID.id == "divcbMale") ? true : false;
    $("cbFemale").checked = (eID.id == "divcbFemale") ? true : false;
}
//xử lý đăng ký
function SignUp() {
    var i = 0;
    if ($('txtSUName') && utils.trim($('txtSUName').value) == "") {
        //$('txtSUName').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][11], 'block');
        i = 1;
    }
    else if ($('txtSUNick') && (utils.trim($('txtSUNick').value).length <6 || utils.trim($('txtSUNick').value).length > 15)) {
        //$('txtSUNick').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][18], 'block');
        i = 1;
    }
    else if ($('txtSUNick') && $('txtSUNick').value.match(/\s/g)) {
        //$('txtSUNick').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][20], 'block');
        i = 1;
    }
    else if ($('txtSUNick') && ClickRegExpChar($('txtSUNick').value) == false) {
        //$('txtSUNick').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][19], 'block');
        i = 1;
    }
    else if ($('txtSUEmail') && utils.trim($('txtSUEmail').value) == "") {
        showErrorRegister('msgError', 'pMessDK pMessFail', "Bạn chưa nhập địa chỉ email", 'block');
        i = 1;
    } 
    else if ($('txtSUEmail') && checkMail($('txtSUEmail').value) == false) {
        //$('txtSUEmail').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][12], 'block');
        i = 1;
    }
    else if ($('txtSUPass') && (utils.trim($('txtSUPass').value).length < 6 || utils.trim($('txtSUPass').value).length > 15)) {
       // $('txtSUPass').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][21], 'block');
        i = 1;
    }
    else if ($('txtSUPass') && $('txtSURePass').value != $('txtSUPass').value) {
        //$('txtSURePass').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][3], 'block');
        i = 1;
    }
    else if ($('cbFemale') && ($('cbFemale').checked == false && $('cbMale').checked == false)) {
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][10], 'block');
        i = 1;
    }
    else if ($('ddlMonth') && ($('ddlYear').selectedIndex == 0 || $('ddlMonth').selectedIndex == 0 || $('ddlDate').selectedIndex == 0)) {
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][4], 'block');
        i = 1;
    }
    else if ($("cbAccept") && $("cbAccept").checked == false) {
        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][16], 'block');
        i = 1;
    }
//    else if ($("agreement") && $("agreement").checked == false) {
//        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][17], 'block');
//        i = 1;
//    }
    else if (i == 0) {
        $('btnSignUp').onclick = "";
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }        
        $('form1').action = strUrlLink + strStateNM + "/register.aspx";
        $('form1').submit();
        return false;
    }
}
/***************************************************************************************************************************/
// xử lý đăng ký nhanh
function SignUpBuy() {
    var i = 0;
    if ($('txtSUBName') && utils.trim($('txtSUBName').value) == "") {
        //$('txtSUBName').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "en" ? langSU_en()[0][11] : langSU()[0][11]), 'block');
        i = 1;
    }
    else if ($('txtSUBEmail') && checkMail($('txtSUBEmail').value) == false) {
        //$('txtSUBEmail').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "en" ? langSU_en()[0][12] : langSU()[0][12]), 'block');
        i = 1;
    }
    else if ($('txtSUBPass') && (utils.trim($('txtSUBPass').value).length < 6 || utils.trim($('txtSUBPass').value).length > 15)) {
        //$('txtSUBPass').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "en" ? langSU_en()[0][21] : langSU()[0][21]), 'block');
        i = 1;
    }
    else if ($('txtSUBPass') && $('txtSUBRePass').value != $('txtSUBPass').value) {
        //$('txtSUBRePass').focus();
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "en" ? langSU_en()[0][3] : langSU()[0][3]), 'block');
        i = 1;
    }
    //    else if ($("cbAccept") && $("cbAccept").checked == false) {
    //        showErrorRegister('msgError', 'pMessDK pMessFail', langSU()[0][16], 'block');
    //        i = 1;
    //    }
    else if ($("agreement") && $("agreement").checked == false) {
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "en" ? langSU_en()[0][17] : langSU()[0][17]), 'block');
        i = 1;
    }
    else if (i == 0) {
        $('btnSUBuy').onclick = "";
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }
        $('form1').action = strUrlLink + strStateNM + "/register-buy.aspx?id=" + getQueryStringValue("id") + (strLangNM == "en" ? "&lang=en" : "");
        $("form1").submit();
        return false;
    }
}
/***************************************************************************************************************************/
function checkNickName() {
    var error = 0;
    if ($("txtSUNick").value != "") {        
        if ($('txtSUNick') && (utils.trim($('txtSUNick').value).length <6 || utils.trim($('txtSUNick').value).length > 15)) {
            error = 1;
            showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "vi") ? langSU()[0][18] : langSU_en()[0][18], 'block');
        }
        else if ($('txtSUNick') && $('txtSUNick').value.match(/\s/g)) {
            error = 1;
            showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "vi") ? langSU()[0][20] : langSU_en()[0][20], 'block');
        }
        else if ($('txtSUNick') && ClickRegExpChar($('txtSUNick').value) == false) {
            error = 1;
            showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "vi") ? langSU()[0][19] : langSU_en()[0][19], 'block');
        }
    }
    else {
        error = 1;
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "vi") ? langSU()[0][14] : langSU_en()[0][14], 'block');
    }
    if (error == 0) {
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }
        utils.call(strUrlLink + strStateNM + "/register.aspx", { "flag": "checknickname", "nickname": $('txtSUNick').value }, "POST", "resultCheckNickName", "resultCheckNickName");
    }

}

function resultCheckNickName(result) {
    if (result == "1") {
        showErrorRegister('msgError', 'pMessDK pMessFail', (strLangNM == "vi") ? langSU()[0][1] : langSU_en()[0][1], 'block');
        $('txtSUNick').focus();
    }
    else {
        $("pError").setStyle({ height: "0px", display: "none" });
    }
}